/********************************************************/
/*	Author:		gong_libin			*/
/*	Date:		2014_08_25			*/
/*	File:		GtDrRecv.cpp			*/
/********************************************************/

#include "GtMsg.h"
#include "GtPro.h"
#include "GtTime.h"
#include "GtDcpDef.h"

#define	GT_DR_KVS			32

void GtDrRecvCore(GtIpc::CGtMsg* pCMsg, bool bPrint)
{
	int iCount = 0;
	GTDCPMSG_S stMsg = {0, {'\0'}};
	GtNet::CGtPro CPro;
	GtCls::CGtTime CTime;
	GtNet::GTPROKV_S stKv[GT_DR_KVS] = {{'\0', 0, NULL}};

	while (true) {
		while ((pCMsg + iCount)->GtMsgGet(&stMsg, sizeof(GTDCPMSG_S), GT_IPC_NOWAIT) > 0) {
			if (true == bPrint) {
				CTime.GtTimeOn();
				GT_PRINT("IPCMSG[%d]:\n", iCount);
			}

			CPro.GtProParse(stMsg.m_uszMsg, stMsg.m_iLength);
			GtProKvInput(stKv, CPro.GtProGetTotal(), CPro.GtProGetBody(), CPro.GtProGetLength());

			if (true == bPrint) {
				CPro.GtProDisplay();
				GtProKvDisplay(stKv, CPro.GtProGetTotal());
				GT_PRINT("Length: %d + Msg: ######\n", stMsg.m_iLength);
			}

			if (true == bPrint) {
				CTime.GtTimeOff();
				CTime.GtTimeDisplay();
			}

			memset(&stMsg, '\0', sizeof(GTDCPMSG_S));
		}
		if (GT_DD_MSG == ++ iCount) {
			usleep(10000);
			iCount = 0;
		}
	}

	return;
}

int main(int argc, char* argv[])
{
	int iCount = 0;
	bool bPrint = false;
	GtIpc::CGtMsg* pCMsg = NULL;

	if (argc > 1) {
		bPrint = true;
	}

	pCMsg = new GtIpc::CGtMsg[GT_DD_MSG];
	for (iCount = 0; iCount < GT_DD_MSG; iCount ++) {
		if (GT_SUCCESS != (pCMsg + iCount)->GtIpcCreate((pCMsg + iCount)->GtIpcKey(GT_DD_PATH, GT_DD_KEY + iCount))) {
			GT_ERROR("Failed to GtIpcCreate\n");
			return GT_FAILURE;
		}
	}

	GtDrRecvCore(pCMsg, bPrint);

	return GT_SUCCESS;
}
